# -*- coding: utf-8 -*-
import os
import hou
from liberHook.BaseHook import BaseHook
from liberLibs.dccLibs import Houdini
from liberLibs.utils import write_json


class Hook(BaseHook):
    def __init__(self, library, directory, ext, start, end):
        BaseHook.__init__(self, library, directory, ext, start, end)

    def execute(self):
        sels = Houdini.selected()
        if len(sels) > 1:
            self.append_error("More than one node selected.")
            return
        node = sels[0]
        node.setName(self.name)
        category = node.type().category().name()
        path = node.path()
        self.write_path(category, path)
        Houdini.create_hda(self.file)

    def write_path(self, category, node_path):
        json_path = "%s/path.json" % self.location
        json_path = json_path.replace("\\", "/")
        data = dict(category=category, path=node_path)
        write_json(json_path, data)
